//
//  HRMineHeadView.m
//  Hear
//
//  Created by mac on 2017/6/14.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRMineHeadView.h"
#import "HRCoverImageModel.h"
#import "HRChangeBackImageViewController.h"

@interface HRMineHeadView ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong)UIImageView *iconImageView;
@property (nonatomic, strong)UILabel *nickNameLabel;
@property (nonatomic, strong)UILabel *numLabel;

@end
@implementation HRMineHeadView

-(void)setUserInfoModel:(HRUpdataUserInfoModel *)userInfoModel{
    _userInfoModel = userInfoModel;
    
    NSArray *arr = [userInfoModel.UserCoverPicture jsonValueDecoded];
    
    if (!arr.count){
        self.scrollView.localizationImageNamesGroup = @[@"默认主页背景图"];
    }else{
        self.scrollView.imageURLStringsGroup = arr;
    }

    self.nickNameLabel.text = userInfoModel.NickName;

    self.numLabel.text = [NSString stringWithFormat:@"ID：%@",userInfoModel.UserNo];
    
    
    [[EMClient sharedClient] updatePushNotifiationDisplayName:userInfoModel.NickName completion:^(NSString *aDisplayName, EMError *aError) {
        
    }];
    WeakSelf
    if (userInfoModel.UserPictureBackgroundColor.length){
        [HRCoverImageModel OriginImage:[UIImage imageNamed:userInfoModel.UserPictureName] scaleToSize:CGSizeMake(75, 75) callBackOriginImage:^(UIImage *originImage) {
            weakSelf.iconImageView.image = originImage;
        }];
        self.iconImageView.backgroundColor = [UIColor colorWithHexString:userInfoModel.UserPictureBackgroundColor];
    }else{
        [self.iconImageView setImageWithURL:[NSURL URLWithString:userInfoModel.UserPictureName] placeholder:[UIImage imageNamed:@""]];
    }

}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        WeakSelf
        
//        SDCycleScrollView *scrollView = [SDCycleScrollView new];
//        [self addSubview:(self.scrollView = scrollView)];
//        scrollView.currentUserID = USERID;
//        scrollView.delegate = self;
//        scrollView.placeholderImage = [UIImage imageNamed:@"默认加载图"];
        
        self.iconImageView = [UIImageView new];
        self.iconImageView.clipsToBounds = YES;
        self.iconImageView.layer.cornerRadius = 75/2;
        [self addSubview:self.iconImageView];
        
        
//        if ([USERID isEqualToString:@"admin"]){
//            weakSelf.iconImageView.image = [UIImage imageNamed:@"小助手"];
//        }else{
//            [HRCoverImageModel OriginImage:[UIImage imageNamed:ICONNAME] scaleToSize:CGSizeMake(75, 75) callBackOriginImage:^(UIImage *originImage) {
//                weakSelf.iconImageView.image = originImage;
//            }];
//        }
        
        
        
        self.iconImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapIcon = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            if (weakSelf.clickIconBlock){
                weakSelf.clickIconBlock();
            }
        }];
        [self.iconImageView addGestureRecognizer:tapIcon];
        
        
        
        self.nickNameLabel = [UILabel new];
        self.nickNameLabel.textColor = CustomColor(34, 34, 34, 1);
        self.nickNameLabel.font = CustomUIFont(13);
        [self addSubview:self.nickNameLabel];
        
        self.numLabel = [UILabel new];
        self.numLabel.textColor = CustomColor(160, 160, 160, 1);
        self.numLabel.font = CustomUIFont(11);
        [self addSubview:self.numLabel];

        
//        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.left.right.bottom.equalTo(weakSelf);
//        }];
        
        
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@20);
            make.centerY.equalTo(weakSelf);
            make.size.mas_equalTo(CGSizeMake(75, 75));
        }];
        
        [self.nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.iconImageView.mas_centerY).offset(-10);
            make.left.equalTo(weakSelf.iconImageView.mas_right).offset(15);
            make.right.equalTo(weakSelf).offset(-20);
            make.height.equalTo(@20);
        }];
        
        [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.nickNameLabel.mas_bottom);
            make.right.left.equalTo(weakSelf.nickNameLabel);
            make.height.equalTo(@20);
        }];
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadIconAndBackGroundNotififation) name:@"reloadIconAndBackGroundNotififation" object:nil];
    }
    return self;
}

- (void)reloadIconAndBackGroundNotififation{
    
    [self.iconImageView setImageWithURL:[NSURL URLWithString:ICONNAME] placeholder:[UIImage imageNamed:@""]];
    self.nickNameLabel.text = NICKNAME;

}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    HRChangeBackImageViewController *VC = [HRChangeBackImageViewController new];
    [VIEWCCONTROLER.navigationController pushViewController:VC animated:YES];
}

@end
